package com.ticket;

/****
 *  使用synchronized (this)加锁
 */
public class ThreadTrain2 implements Runnable {
	// 这是货票总票数,多个线程会同时共享资源
	private int trainCount = 100;

	@Override
	public void run() {
		while (trainCount > 0) {// 循环是指线程不停的去卖票
			try {
				// 等待100毫秒
				Thread.sleep(10);
			} catch (InterruptedException e) {
			}
			sale();
		}
	}

	/**
	 * @methodDesc: 功能描述:(出售火车票)
	 */
	public void sale() {
		synchronized (this) {
			if (trainCount > 0) {
				try {
					Thread.sleep(10);
				} catch (Exception e) {

				}
				System.out.println(Thread.currentThread().getName() + ",出售 第" + (100 - trainCount + 1) + "张票.");
				trainCount--;
			}
		}
		
	}
	
	public static void main(String[] args) {
		ThreadTrain2 threadTrain = new ThreadTrain2(); // 定义 一个实例
		Thread thread1 = new Thread(threadTrain, "一号窗口");
		Thread thread2 = new Thread(threadTrain, "二号窗口");
		thread1.start();
		thread2.start();
	}
}
